package io.xxx.xbutler.core.callback;

import com.alibaba.fastjson2.JSONObject;
import io.xxx.xbutler.domain.Callback;
import io.xxx.xbutler.domain.RobotRoomLog;
import io.xxx.xbutler.domain.RoomType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 机器人被踢出群回调
 * <p>
 * 机器人被踢出群，通过本接口回调给到商家，被踢出群后，平台会自动取消关注该群，并将取消关注状态通过机器人退群自动取消关注群回调接口给到商家。
 * 机器人被踢出群达到一定次数后将影响其他接口的限制；
 *
 * <pre>
 * {
 *     "vcMerchantNo": "",
 *     "vcRobotWxId": "",
 *     "vcRobotSerialNo": "",
 *     "vcSerialNo": "20220728181810970410126619637",
 *     "nType": 4507,
 *     "nResult": 1,
 *     "vcResult": "SUCCESS",
 *     "nTimeStamp": 1659003491640,
 *     "Data": {
 *         "vcChatRoomId": "111@chatroom",
 *         "vcChatRoomSerialNo": "",
 *         "vcNickName": "菜鸟驿站 1111",
 *         "vcBase64NickName": "",
 *         "IsEnterpriseChatroom": false
 *     }
 * }
 * </pre>
 */
@Slf4j
@Component
public class _4507Handler extends RobotLeaveRoomHandler {

    @Override
    public String name() {
        return "机器人被踢出群回调";
    }

    @Override
    public boolean supports(int type) {
        return type == 4507;
    }

    @Override
    protected RobotRoomLog buildRobotRoomLog(Callback callback) {
        String robotId = callback.getRobotId();
        String robotSerialNo = callback.getSerialNo();
        JSONObject data = (JSONObject) callback.getData();
        String roomId = data.getString("vcChatRoomId");
        return new RobotRoomLog()
                .setId(idGenerator.next())
                .setRobotId(robotId)
                .setRobotSerialNo(robotSerialNo)
                .setRobotName(data.getString("vcNickName"))
                .setBase64RobotName(data.getString("vcBase64NickName"))
                .setRoomId(roomId)
                .setRoomSerialNo(data.getString("vcBase64NickName"))
                .setRoomType(data.getBoolean("IsEnterpriseChatroom") ? RoomType.WECOM : RoomType.WECHAT)
                .setType(21)
                .setTime(callback.getTimestamp())
                .setCreatedTime(LocalDateTime.now());
    }
}
